/**
 * Created by Administrator on 2017/9/16.
 */

import Vue from 'vue'
import Vuex from 'vuex'
import data from 'assets/json/data.json'
import * as types from './mutationNames'

Vue.use(Vuex)

//定义state
const state = {
                data: data, //货品信息
                cart: []}   //购物车--为{food: '', price: '', quantity: 0}的数组

//定义getters -- 相当于Store的计算属性
const getters = {
    seller: function (state, getters) {
      return state.data.seller
    },
    goods: function (state, getters) {
      return state.data.goods
    },
    ratings: function (state, getters) {
      return state.data.ratings
    },
    quantityOfFoodInCart: function (state, getters) {
      return foodName=> {
        let food = state.cart.find(item => item.food === foodName)
        return food ? food.quantity : 0
      }
    },
    countOfItemInCart: function (state, getters) {
      return state.cart.reduce((prev, item)=>{ return prev + item.quantity },0)
    },
    costOfItemInCart: function (state, getters) {
      let cost = 0.0
      state.cart.forEach(item => {
          cost += item.price * item.quantity
      })

      return cost
    },
    itemsInCart: function (state, getters) {
       return state.cart
     },
    getRatings: function (state, getters) {
      return (rateType, contentOnly) => {
        let ratings = []
        if(rateType === 2){
          ratings = state.data.ratings
        } else {
          ratings = state.data.ratings.filter(rating => rating.rateType === rateType)
        }

        if(contentOnly){
          ratings = ratings.filter(rating=>rating.text !== "")
        }

        return ratings
      }
    }
}

//Mutations
//作用：同步调用 store.commit 方法，更改 Vuex 的 store 中的状态的唯一方法
const mutations = {
  [types.ADD_TO_CART] (state, foodName) {
    let item = state.cart.filter(item=>item.food === foodName)
    if(item.length <1) {
      let food = undefined
      for(let i = 0; i < state.data.goods.length; i++){
        let found = state.data.goods[i].foods.find(food=>food.name === foodName)
        if(found){
          food = found
          break
        }
      }
      if(food) {
        state.cart.push({food: foodName, price: food.price, quantity: 1})
      }
    } else {
      item[0].quantity++;
    }
  },
  [types.REMOVE_FROM_CART] (state, foodName) {
    for(let i = 0; i < state.cart.length; i++){
      let item = state.cart[i]
      if(item.food === foodName){
        if(item.quantity > 1){
          item.quantity--
        } else {
          state.cart.splice(i, 1)
        }

        break
      }
    }
  },
  [types.CLEART_CART](state){
    state.cart = []
  }
}

//Actions
//作用：1.提交的是 mutation，而不是直接变更状态
//     2.可以包含任意异步操作
const actions = {
    addToCart: function ({commit}, foodName) {

      commit(types.ADD_TO_CART, foodName)
    },
    removeFromCart: function ({commit}, foodName) {
      commit(types.REMOVE_FROM_CART, foodName)
    },
    clearCart: function ({commit}) {
      commit(types.CLEART_CART)
    }
}

//导出Store
export default new Vuex.Store({
    state,
    getters,
    mutations,
    actions,
    modules:{
    }
});
